import { isURL } from '@/utils/validate'
// import Layout from '@/views/layout/Layout'

/**
 * 过滤对象中为空的属性
 * @param obj
 * @returns {*}
 */
export function filterObj(obj) {
  if (!(typeof obj == 'object')) {
    return
  }

  for (let key in obj) {
    if (
      obj.hasOwnProperty(key) &&
      (obj[key] == null || obj[key] == undefined || obj[key] === '')
    ) {
      delete obj[key]
    }
  }
  return obj
}

/**
 * 清空对象中的属性值 改为undefined
 * @param obj
 * @returns {*}
 */
export function clearObj(obj) {
  if (!(typeof obj == 'object')) {
    return
  }

  for (let key in obj) {
    if (obj.hasOwnProperty(key) && obj[key] !== undefined) {
      obj[key] = undefined
      // console.log(key + "clear ing");
    }
  }
  return obj
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent() {
  let event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

/**
 * 加载首页路由
 * @param data
 * @returns {*[]}
 */
export function generateIndexRouter(data) {
  // let indexRouter = [
  //   {
  //     path: '/',
  //     name: 'dashboard',
  //     //component: () => import('@/components/layouts/BasicLayout'),
  //     component: resolve => require(['@/views/layout/Layout'], resolve),
  //     meta: {title: '首页', icon: 'dashboard', noCache: true, affix: true},
  //     redirect: '/dashboard',
  //     children: [
  //       ...generateChildRouters(data)
  //     ]
  //   }, {
  //     "path": "*", "redirect": "/404", "hidden": true
  //   }];
  let indexRouter = generateChildRouters(data)
  indexRouter.push({ path: '*', redirect: '/404', hidden: true })
  return indexRouter
}

function generateChildRouters(data) {
  const routers = []
  for (let item of data) {
    let component = 'views/' + item.component

    let URL = (item.meta.url || '').replace(/{{([^}}]+)?}}/g, (s1, s2) =>
      eval(s2)
    ) // URL支持{{ window.xxx }}占位符变量
    if (isURL(URL)) {
      item.meta.url = URL
    }
    // let componentPath = resolve => require(['@/' + component+'.vue'], resolve);
    let menu = {
      path: item.path,
      name: item.name,
      redirect: item.redirect,
      component: (resolve) => require(['@/' + component + '.vue'], resolve),
      hidden: item.hidden,
      //component:()=> import(`@/views/${item.component}.vue`),
      meta: {
        title: item.meta.title,
        icon: item.meta.icon,
        url: item.meta.url,
        permissionList: item.meta.permissionList,
        keepAlive: item.meta.keepAlive,
        /*update_begin author:wuxianquan date:20190908 for:赋值 */
        // internalOrExternal:item.meta.internalOrExternal
        /*update_end author:wuxianquan date:20190908 for:赋值 */
      },
    }
    if (item.alwaysShow) {
      menu.alwaysShow = true
      menu.redirect = menu.path
    }
    if (item.children && item.children.length > 0) {
      menu.children = [...generateChildRouters(item.children)]
    }
    //--update-begin----author:scott---date:20190320------for:根据后台菜单配置，判断是否路由菜单字段，动态选择是否生成路由（为了支持参数URL菜单）------
    //判断是否生成路由
    if (item.route && item.route === '0') {
      //console.log(' 不生成路由 item.route：  '+item.route);
      //console.log(' 不生成路由 item.path：  '+item.path);
    } else {
      routers.push(menu)
    }
    //--update-end----author:scott---date:20190320------for:根据后台菜单配置，判断是否路由菜单字段，动态选择是否生成路由（为了支持参数URL菜单）------
  }
  return routers
}
